/**
 * 角色实体类
 */
cc.Class({
    extends: cc.Component,

    properties: {
        /**
         * 跳跃到下一圈的时间
         */
        jumpTime: {
            default: 0,
            tooltip: "跳跃到下一圈的时间"
        },
        /**
         * 地图管理器节点
         */
        mapManagerNode: cc.Node,
        /**
         * 所在的圆圈节点
         */
        currentCircle: {
            default: null,
            type: cc.Node,
            visible: false
        },
        /**
        * 游戏管理器
        */
        game: {
            default: null,
            visible: false
        },
        /**
        * 结束管理器
        */
        over: {
            default: null,
            visible: false
        },
        /**
         * 跳跃音效
         */
        jumpAudio: cc.AudioClip,
    },

    onLoad () {
        this._animation = this.node.getComponent(cc.Animation);
        //渲染节点
        this._renderer = this.node.getChildByName('Renderer');
        this._mapManager = this.mapManagerNode.getComponent('MapManager');
        
        this._isJumping = false;
        this._isAlive = true;

        this._shield = this._renderer.getChildByName('PlayerShield');
    },

    start () {
        //出生在外圈上
        this._renderer.setPosition(0, this.currentCircle.width / 2);
        this.updateCircle();
    },

    update (dt) {
        //跟随移动旋转
        //this.node.rotation += this._rotateSpeed * dt;
        this.node.rotation = this._isJumping ? this.node.rotation : this._rotationDelta + this._currentCircleItem.getRotation();
        var pos = this._isJumping ? this._renderer.getPositionY() - this._mapManager.getSpeed() * dt :
        this.currentCircle.width * this.currentCircle.scaleX / 2;
        this._renderer.setPositionY(pos);
        //跟随缩放
        this._renderer.scaleX = this._renderer.scaleY = this.currentCircle.scaleX;
    },

    /**
     * 更新跳跃到的新圈
     */
    updateCircle () {
        //更新自身所在的圈数
        this._currentId = this.currentCircle.getComponent('CircleLineItem')._id;
        if (this._currentId > 0) {
            this.game.showScore(this._currentId);
        }
        

        this._rotateSpeed = this.currentCircle.getComponent('CircleLineItem').rotateSpeed;
        //this._movingSpeed = this._mapManager.getSpeed();

        //相对旋转角度
        this._currentCircleItem = this.currentCircle.getComponent('CircleLineItem');
        this._rotationDelta = this.node.rotation - this._currentCircleItem.getRotation();
        
        this._isJumping = false;
        if (this._mapManager.isInCanJumpCircle(this.currentCircle)) {
            this._mapManager.setAllCircleInner();
        }
        
    },

    /**
     * 往外圈跳跃
     */
    jump () {
        if (this._isJumping || this._mapManager._isInner || !this._isAlive) return;
        //跳跃结束设置所在的圈
        var canJump = this._mapManager.setPlayerNextCircle();
        if (!canJump) return;
        //暂停旋转速度
        //this._rotateSpeed = 0;
        this._isJumping = true;
        var moveAnim = cc.moveBy(this.jumpTime, cc.v2(0, this._mapManager.gap / 2));
        var scaleAnim = cc.scaleTo(this.jumpTime, this.currentCircle.scaleX, this.currentCircle.scaleY);
        this._renderer.runAction(cc.spawn(moveAnim, scaleAnim));
        
        //更新速度
        this.scheduleOnce(this.updateCircle, this.jumpTime);
    },

    /**
     * 角色死亡
     */
    die () {
        this._isAlive = false;
        //停止运动
        this._mapManager._isGameOver = true;
        this._mapManager.stopAllCircle();
        //角色闪烁
        this._animation.play('player_die');
    },

    /**
     * 死亡动画播放完毕
     */
    dieOver () {
        //显示结束界面
        this.over.showOverPanel(this._currentId);
    },

    /**
     * 打开护盾
     */
    openShield () {
        this._shield.active = true;
        //持续一段时间后关闭
        this.scheduleOnce(function(){
            this._shield.active = false;
        }, 10);
    },

    /**
     * 播放音效
     * @param {cc.AudioClip}} clip 音频片段
     */
    playAudio (clip) {
        if(clip != null){
            cc.audioEngine.play(clip, false, 1);
        }
    }
});
